<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="searchData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="init" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="index" width="80" label="序号" align="center" />

          <el-table-column prop="qjrName" label="姓名" align="center" />
          <el-table-column prop="context" label="内容" align="center" />
          <el-table-column prop="createTime" label="创建时间" align="center" />
          <el-table-column prop="startTime" label="开始时间" align="center" />
          <el-table-column prop="endTime" label="结束时间" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template #default="scope">
              <div v-if="scope.row.status == 0">
                <el-tag type="warning">审核中</el-tag>
              </div>
              <div v-else-if="scope.row.status == 1">
                <el-tag type="warning">批准</el-tag>
              </div>
              <div v-else>
                <el-tag type="danger">不批准</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <ElPagination
        ref="tableRef"
        :total="pagination.total"
        @sizeChange="onPageSizeChange"
        @currentChange="onCurrentChange"
      />
    </el-card>
    <!-- 新增/修改 -->
    <el-dialog v-model="dialogVisible" :title="formData.id == 0 ? '新增' : '修改'" @closed="resetForm" width="30%">
      <el-form ref="formRef" :model="formData" label-width="100px" label-position="left">
        <el-form-item label="事项">
          <el-input v-model="formData.uid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="formData.context" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="formData.startTime"
            type="datetime"
            placeholder="请选择"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="formData.endTime"
            type="datetime"
            placeholder="请选择"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="请假原因">
          <el-input v-model="formData.qjr" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formData.status" placeholder="Select" size="large" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue"
import {
  getTableDataApi,
  updateTableDataApi,
  createTableDataApi,
  deleteTableDataApi
} from "@/api/askForLeaveManage/index"
import { type FormInstance, ElMessage, ElMessageBox } from "element-plus"
import { Search, Refresh, CirclePlus, RefreshRight } from "@element-plus/icons-vue"
import ElPagination from "@/components/ElPagination/index.vue"
import { throttle } from "@/utils/setTimeoutOperation"

// defineOptions({
//   // 命名当前组件
//   name: "AskForLeaveManage"
// })

const searchData = reactive({
  username: ""
})

const handleSearch = () => {
  init()
}
const resetSearch = () => {
  searchData.username = ""
  handleSearch()
}

const options = [
  {
    value: 0,
    label: "审核中"
  },
  {
    value: 1,
    label: "批准"
  },
  {
    value: 2,
    label: "不批准"
  }
]

const loading = ref<boolean>(false)
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = reactive({
  id: 0,
  uid: "",
  context: "",
  startTime: "",
  endTime: "",
  qjr: "",
  status: 0
})

// 增 改
const handleCreateOrUpdate = () => {
  loading.value = true
  let dataList = {}
  if (formData.id == 0) {
    dataList = {
      uid: formData.uid,
      context: formData.context,
      startTime: formData.startTime,
      endTime: formData.endTime,
      qjr: formData.qjr,
      status: formData.status
    }
    createTableDataApi(dataList)
      .then(() => {
        ElMessage.success("操作成功")
        dialogVisible.value = false
        init()
      })
      .finally(() => {
        loading.value = false
      })
  } else {
    dataList = {
      id: formData.id,
      uid: formData.uid,
      context: formData.context,
      startTime: formData.startTime,
      endTime: formData.endTime,
      qjr: formData.qjr,
      status: formData.status
    }
    updateTableDataApi(dataList)
      .then(() => {
        ElMessage.success("操作成功")
        dialogVisible.value = false
        init()
      })
      .finally(() => {
        loading.value = false
      })
  }
}
const resetForm = () => {
  formData.id = 0
  formData.uid = ""
  formData.context = ""
  formData.startTime = ""
  formData.endTime = ""
  formData.qjr = ""
  formData.status = 0
}

//#region 删
const handleDelete = (row: any) => {
  console.log("row==", row)
  ElMessageBox.confirm(`正在删除用户：${row.name}，确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    const deleteData = [{ id: row.id }]
    deleteTableDataApi(deleteData).then(() => {
      ElMessage.success("删除成功")
      init()
    })
  })
}

const handleAdd = () => {
  dialogVisible.value = true
}
// 改
const handleUpdate = (row: any) => {
  dialogVisible.value = true
  formData.id = row.id
  formData.uid = row.uid
  formData.context = row.context
  formData.startTime = row.startTime
  formData.endTime = row.endTime
  formData.qjr = row.qjr
  formData.status = row.status
}

// 查
const tableData = ref([])
const init = () => {
  loading.value = false
  getTableDataApi({
    current: pagination.value.current,
    size: pagination.value.pageSize,
    searchKey: "qjrName",
    searchValue: searchData.username
  })
    .then(({ data }) => {
      tableData.value = data.records
      pagination.value.total = data.total
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

// 分页
const tableRef = ref()
const pagination = ref({ current: 1, pageSize: 10, total: 0 })
const onPageSizeChange = throttle((size: number) => {
  pagination.value.pageSize = size
  pagination.value.current = 1
  tableRef.value.currentPage = pagination.value.current
  init()
}, 300)
const onCurrentChange = throttle((current: number) => {
  pagination.value.current = current
  init()
}, 100)

onMounted(() => {
  init()
})
</script>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
